﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using Tireguit.Nmon.UI.Elements.Logical;

namespace Tireguit.Nmon.UI.Elements.Controls
{
    public partial class PlotTree : UserControl
    {
        protected PlotTreeElement _treeelement = null;
        public PlotTreeElement TreeElement
        {
            get { return _treeelement; }
            protected set { _treeelement = value; }
        }
        public Action<string> AddToGraph;
        public Action<string> RemoveFromGraph;
        public IDbConnection Connection { get; protected set; }
        public PlotTree(IDbConnection connection)
        {
            Connection = connection;
            InitializeComponent();
            InitNodes();
        }
        protected void InitNodes()
        {
            _treeelement = new PlotTreeElement(Connection);
            _treeelement.RootElement.Expand();
            treeView1.Nodes.Add(_treeelement.RootElement);
            treeView1.ContextMenuStrip = contextMenuStrip1;
            //treeView1.ExpandAll();
           // treeView1.AfterSelect += new System.Windows.Forms.TreeViewEventHandler(handleselecttree);
        }

        private void expandToolStripMenuItem_Click(object sender, EventArgs e)
        {
            treeView1.SelectedNode.Expand();
            collapseToolStripMenuItem.Enabled = true;
            expandToolStripMenuItem.Enabled = false;

        }

        private void collapseToolStripMenuItem_Click(object sender, EventArgs e)
        {
            treeView1.SelectedNode.Collapse();
            collapseToolStripMenuItem.Enabled = false;
            expandToolStripMenuItem.Enabled = true;

        }

        private void ajouterAuGrapheToolStripMenuItem_Click(object sender, EventArgs e)
        {
            if (treeView1.SelectedNode==null)
            {
                return;
            }
            if (AddToGraph!=null)
            {
                AddToGraph(treeView1.SelectedNode.Name);
            }
            

        }

        private void supprimerDuGrapheToolStripMenuItem_Click(object sender, EventArgs e)
        {
            if (treeView1.SelectedNode == null)
            {
                return;
            }
            if (RemoveFromGraph!=null)
            {
                RemoveFromGraph(treeView1.SelectedNode.Name);
            }

        }

        private void treeView1_AfterSelect(object sender, TreeViewEventArgs e)
        {
            
           
            if ((e.Node.Nodes == null) || (e.Node.Nodes.Count == 0))
            {
                collapseToolStripMenuItem.Enabled = false;
                expandToolStripMenuItem.Enabled = false;
            }
            else
            {
                if (e.Node.IsExpanded)
                {
                    collapseToolStripMenuItem.Enabled = true;
                    expandToolStripMenuItem.Enabled = false;
                }
                else
                {
                    collapseToolStripMenuItem.Enabled = false;
                    expandToolStripMenuItem.Enabled = true;
                }
            }
            if (!e.Node.Name.StartsWith("+S")&&!e.Node.Name.Contains(':'))
            {
                ajouterAuGrapheToolStripMenuItem.Enabled = false;
                supprimerDuGrapheToolStripMenuItem.Enabled = false;
            }
            else
            {
                ajouterAuGrapheToolStripMenuItem.Enabled = true;
                supprimerDuGrapheToolStripMenuItem.Enabled = true;
            }
        }
    }
}
